package com.example.auth.utils;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * Created by wangtao on 2018/3/2.
 */
public class KeyHelper {
    /**
     * 解析公钥
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    public static PublicKey getPublicKey(String fileName) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        InputStream in = classPathResource.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        byte[] keyBytes = new byte[(int) in.available()];
        dis.readFully(keyBytes);
        dis.close();
        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePublic(spec);
    }

    /**
     * 解析密钥
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    public static PrivateKey getPrivateKey(String fileName) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        InputStream in = classPathResource.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        byte[] keyBytes = new byte[(int) in.available()];
        dis.readFully(keyBytes);
        dis.close();
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(spec);
    }

    /**
     * 生成公钥、私钥
     *
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static void generateKey(String publicKeyFileName, String privateKeyFileName, String secret) throws NoSuchAlgorithmException, IOException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        SecureRandom secureRandom = new SecureRandom(secret.getBytes());
        keyPairGenerator.initialize(1024, secureRandom);
        KeyPair keyPair = keyPairGenerator.genKeyPair();
        byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
        FileOutputStream fos = new FileOutputStream(publicKeyFileName);
        fos.write(publicKeyBytes);
        fos.close();
        byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
        fos = new FileOutputStream(privateKeyFileName);
        fos.write(privateKeyBytes);
        fos.close();
    }

    @Test
    public static void main(String[] args) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("/keystore/user/pri.key");
        File file = classPathResource.getFile();
        System.out.println(file.length());
        InputStream inputStream = classPathResource.getInputStream();
        System.out.println(inputStream.available());
        DataInputStream dis = new DataInputStream(inputStream);
        dis.close();
    }
}
